package com.xilihui.xlh.business.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.AppointmentStoresActivity;
import com.xilihui.xlh.business.entities.AgentsEntity;
import com.xilihui.xlh.business.entities.ItemsEntity;
import com.xilihui.xlh.business.entities.ShopEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.rxbus.RxBus;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/23.
 */

public class StoresAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    
    private static final int zhengchang = 1;
    private static final int hint = 2;
    private static final int headhint = 3;
    
    ArrayList<AgentsEntity> datas = new ArrayList<>();
    
    Activity activity;
    
    public StoresAdapter(Activity activity){
        this.activity = activity;
    }
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if(viewType==zhengchang){
            view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_shop,parent,false);
        }else if(viewType==hint){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_appointment_store_line
            ,parent,false);
        }else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_appointment_store_head,
                    parent,false);
        }
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        switch (getItemViewType(position)){
            case zhengchang:
                holder.setText(R.id.tv_shopName,datas.get(position).getName());
                holder.setText(R.id.tv_address,datas.get(position).getAddress()+" | 约"+datas.get(position).getJuli());

                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv_shopLogo);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+datas.get(position).getBackground_picture_url());

                String str = datas.get(position).getScore();
                if(!TextUtil.isEmpty(str)){
                    float score = Float.parseFloat(str);
                    if(score==0){
                        holder.setVisibility(R.id.ratingBar,View.GONE);
                        holder.setText(R.id.tv_pingfeng,"暂无评分");
                    }else {
                        holder.setVisibility(R.id.ratingBar,View.VISIBLE);
                        holder.setRating(R.id.ratingBar,score);
                        holder.setText(R.id.tv_pingfeng,score+"分");
                    }

                }

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        RxBus.getDefault().post(datas.get(position));
                        holder.itemView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                activity.finish();
                            }
                        },1500);
                    }
                });
                
                break;
            case hint:
                
                break;
            case headhint:
                
                break;
        }
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(datas.get(position).getShowType()==1){
            return zhengchang;
        }else if(datas.get(position).getShowType()==2){
            return hint;
        }else {
            return headhint;
        }
    }
    
    public void setData(ArrayList<AgentsEntity> data){
        if(datas!=null&&datas.size()>0){
            datas.clear();
        }
        datas.addAll(data);
        notifyDataSetChanged();
    }
    
}
